#include "../Graphics/2D/core/Scene.h"
#include "../Graphics/2D/shapes/BasicShape.h"
#include "GameEngine.h"
#include <QPointF>

GameEngine::GameEngine() {
	this->scene = new Scene();
	this->render = new Render();
	this->world = new Physic2DWorld(new QPointF(0, -9.8f));
}
void GameEngine::init() {
	elapsedTimer->start();


	RectangleShape* rectangle2 = new RectangleShape(30, 30);
	rectangle2->setColor(new QColor(255, 0, 255));

	this->scene->addShape(rectangle2);
	RectangleBody* rectangleBody2 = new RectangleBody(30, 30);
	rectangleBody2->setPosition(new QPointF(-100.0f, 200.0f));
	rectangleBody2->setMass(1);
	rectangleBody2->setVelocity(new QPointF(0, 100.0f));
	this->world->addBody(rectangleBody2);
	this->bodyMapingShape->insert(rectangleBody2, rectangle2);


	RectangleShape* playerShape = new RectangleShape(30, 30);
	playerShape->setColor(new QColor(255, 0, 255));
	this->scene->addShape(playerShape);

	this->player = new RectangleBody(30, 30);
	player->setPosition(new QPointF(-300.0f, 100.0f));
	player->setMass(1);
	player->setVelocity(new QPointF(3, 10.0f));
	this->world->addBody(player);
	this->bodyMapingShape->insert(player, playerShape);


	RectangleShape* rectangle6 = new RectangleShape(400, 30);
	rectangle6->setColor(new QColor(0, 0, 255));
	this->scene->addShape(rectangle6);

	RectangleBody* rectangleBody6 = new RectangleBody(400, 30);
	rectangleBody6->setPosition(new QPointF(-200, 100));
	rectangleBody6->setMass(0);
	rectangleBody6->setAngle(0);
	rectangleBody6->setVelocity(new QPointF(0, 0));
	this->world->addBody(rectangleBody6);
	this->bodyMapingShape->insert(rectangleBody6, rectangle6);

	RectangleShape* rectangle3 = new RectangleShape(400, 100);
	rectangle3->setColor(new QColor(0, 0, 255));
	this->scene->addShape(rectangle3);

	RectangleBody* rectangleBody3 = new RectangleBody(400, 100);
	rectangleBody3->setPosition(new QPointF(-200, 0));
	rectangleBody3->setMass(0);
	rectangleBody3->setAngle(0);
	rectangleBody3->setVelocity(new QPointF(0, 0));
	this->world->addBody(rectangleBody3);
	this->bodyMapingShape->insert(rectangleBody3, rectangle3);

	RectangleShape* rectangle4 = new RectangleShape(400, 100);
	rectangle4->setColor(new QColor(0, 0, 255));
	this->scene->addShape(rectangle4);

	RectangleBody* rectangleBody4 = new RectangleBody(400, 100);
	rectangleBody4->setPosition(new QPointF(300, 0));
	rectangleBody4->setMass(0);
	rectangleBody4->setAngle(0);
	rectangleBody4->setVelocity(new QPointF(0, 0));
	this->world->addBody(rectangleBody4);
	this->bodyMapingShape->insert(rectangleBody4, rectangle4);

	/*CircleShape* circle1 = new CircleShape(100);
	circle1->setColor(new QColor(255, 0, 255));
	circle1->setPosition(new QPointF(-100, 200));
	this->scene->addShape(circle1);*/

	/*TriangleShape* triangleShape1 = new TriangleShape(QPointF(-100, -100), QPointF(100, -100), QPointF(0, 100));
	triangleShape1->setColor(new QColor(255, 0, 0));
	triangleShape1->setAngle(0);
	triangleShape1->setPosition(new QPointF(-200, 200));
	this->scene->addShape(triangleShape1);
	TriangleBody* triangleBody1 = new TriangleBody(QPointF(-100, -100), QPointF(100, -100), QPointF(0, 100));
	triangleBody1->setPosition(new QPointF(-200.0f, 200.0f));
	triangleBody1->setMass(1);
	triangleBody1->setAngle(0);
	triangleBody1->setVelocity(new QPointF(0, 0));
	this->world->addBody(triangleBody1);
	this->bodyMapingShape->insert(triangleBody1, triangleShape1);*/

	/*RectangleShape* rectangle1 = new RectangleShape(30, 30);
	rectangle1->setPosition(new QPointF(100, 400));
	rectangle1->setColor(new QColor(0, 255, 0));
	this->scene->addShape(rectangle1);
	RectangleBody* rectangleBody1 = new RectangleBody(30, 30);
	rectangleBody1->setPosition(new QPointF(100.0f, 400.0f));
	rectangleBody1->setMass(1);
	rectangleBody1->setAngle(45);
	rectangleBody1->setVelocity(new QPointF(0, 100.0f));
	this->world->addBody(rectangleBody1);
	this->bodyMapingShape->insert(rectangleBody1, rectangle1);*/


	this->render->init(scene);
}

void GameEngine::keyReleaseEvent(QKeyEvent* event) {
	switch (event->key()) {
	case Qt::Key_A:
		//this->player->setMass(2);
		//this->player->setVelocity(new QPointF(0, 40));
		break;
	case Qt::Key_Left:
		this->player->setMass(2);
		this->player->setVelocity(new QPointF(0, this->player->getVelocity()->y()));
		break;
	case Qt::Key_Right:
		this->player->setMass(2);
		this->player->setVelocity(new QPointF(0, this->player->getVelocity()->y()));
		break;
	}

}
void GameEngine::keyPressEvent(QKeyEvent* event) {
	switch (event->key()) {
	case Qt::Key_A:
		this->player->setMass(2);
		this->player->setPosition(new QPointF(this->player->getPosition()->x(), this->player->getPosition()->y() + 2));
		this->player->setVelocity(new QPointF(this->player->getVelocity()->x(), 40));
		break;
	case Qt::Key_Left:
		this->player->setMass(2);
		//this->player->setPosition(new QPointF(this->player->getPosition()->x(), this->player->getPosition()->y() + 2));
		this->player->getVelocity()->setX(-50);
		break;
	case Qt::Key_Right:
		this->player->setMass(2);
		//this->player->setPosition(new QPointF(this->player->getPosition()->x(), this->player->getPosition()->y() + 2));
		this->player->getVelocity()->setX(50);
		break;
	}

}
void GameEngine::doKeyEvent() {
}


void GameEngine::loopGame() {
	float t = elapsedTimer->elapsed();
	elapsedTimer->restart();
	doKeyEvent();
	this->world->update(t);
	updateShapePhysic();
	this->render->render();
}
